package edu.alanhome.portablelib;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class searchResult extends Activity implements ListView.OnScrollListener  {
	SimpleAdapter adapter;
	String search;
	String doctype;
	String strsearchtype;
	String matchflag;
	String sort;
	int cnt;
	dataFetcher DF;
	InforNode INode;
	int startpos;
	ListView listview;
	ArrayList< Map<String, Object> >  data;
	ProgressDialog mydialog;
	//因为getnextpage会拥塞住，如果放到主线程中，progressdialog不能显示
	Handler handler = new Handler()
	{

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);

			adapter = new SimpleAdapter(
					searchResult.this,
					data,
					R.layout.list,
					new String[]{"BookName","BookNum","BookStore","BookLendable"},
					new int[]{R.id.LTextView01,R.id.LTextView02,R.id.LTextView03,R.id.LTextView04});
			listview.setAdapter(adapter);
			setContentView(listview);
			setProgressBarIndeterminateVisibility(false);
			mydialog.cancel();
			setTitle("共搜索到 "+cnt+" 条记录");
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		listview = new ListView(this);
		setContentView(listview);
		setProgressBarIndeterminateVisibility(true);
		setTitle("正在查询书目信息...");
		DF = new dataFetcher();
		data = new ArrayList< Map<String, Object>>();
		

		Intent it = getIntent();
		Bundle bundle = it.getExtras();
		search  = bundle.getString("search");
		doctype = bundle.getString("doctype");
		strsearchtype = bundle.getString("strsearchtype");
		matchflag = bundle.getString("matchflag");
		sort = bundle.getString("sort");
		mydialog = ProgressDialog.show(searchResult.this,"正在查询书目信息","请稍候 : ) ...");
		new Thread(){
			public void run()
			{
				process();
			}
		}.start();
	}
	void process()
	{
	
		try {
			DF.setSearchstr(search);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		DF.setDocType(doctype);
		DF.setStrsearchType(strsearchtype);
		DF.setMatchFlag(matchflag);
		DF.setSort(sort);
		
				
		try {
			startpos = DF.getNextPage();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cnt = DF.getcount();
		
		Map<String, Object> item = null;
		int sz = DF.getPageSize();
		for(int i=0;i < sz;++i)
		{
			INode = DF.getItem(startpos+i);
			item = new HashMap<String , Object>();
			item.put("BookName", INode.bookname);
			item.put("BookNum" , INode.booknum);
			item.put("BookStore",INode.bookstore);
			item.put("BookLendable",INode.booklendable);
			data.add(item);
		}
		handler.sendMessage(new Message());		
	}
	
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}
	
	
}
